package chapter06;

import java.util.ArrayList;
import java.util.List;

public class MyAtomicIntegerTest extends MyAtomicInteger implements Account{
    public static void main(String[] args) {
        MyAtomicIntegerTest myAtomicIntegerTest = new MyAtomicIntegerTest(100000);
        Account.demo(myAtomicIntegerTest);
    }
    public MyAtomicIntegerTest(int money){
        this.value=money;
    }
    @Override
    public int getBlance() {
        return getValue();
    }

    @Override
    public void withDraw(int amount) {
        decrement(amount);
    }
}
interface Account{
    int getBlance();
    void withDraw(int amount);
    public static void demo(Account account){
        List<Thread> list=new ArrayList<>();
        // 取钱和获取钱
        long begin=System.currentTimeMillis();
        // 创建线程
        for(int i=1;i<=10000;i++){
            list.add(new Thread(()->{

                // 提取10元
                account.withDraw(10);

            }));
        }
        list.forEach(Thread::start);
        for (Thread thread : list) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        long end=System.currentTimeMillis();
        System.out.println("余额："+account.getBlance()+" 运行时间:"+(end-begin));
    }
}